package com.webserver.http;
import static com.webserver.http.HttpContext.CR;
import static com.webserver.http.HttpContext.LF;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequest {
    //请求行相关信息
    private String method;//请求方式
    private String uri;//抽象路径
    private String protocol;//协议版本
    private String requestURI;//uri中的请求部分,即?左侧的内容
    private String queryString;//uri中的参数部分,即?右侧的内容
    //key:参数名 value:参数值
    private  Map<String,String> parameters=new HashMap<>();//每一组参数
//消息头相关信息
    private Map<String,String> headers=new HashMap<>();

    private Socket socket;

    public HttpServletRequest(Socket socket) throws IOException, EmptyRequestException {
        this.socket=socket;
        //1.1解析请求行
        parseRequesLine();
        //1.2解析消息头
        parseHeaders();
        //1.3解析请求行
        parseContent();

    }
    //解析请求行
    private void parseRequesLine() throws IOException, EmptyRequestException {
        String line=readLine();
        //判定是否为空的请求
        if (line.isEmpty()){
            throw new EmptyRequestException("空请求!");
        }
        System.out.print("请求行内容:"+line);

        String[] data= line.split("\\s");
        method=data[0];
        uri=data[1];
        protocol=data[2];
        //进一步解析uri
        parseURI();

        System.out.println("method"+method);
        System.out.println("uri"+uri);
        System.out.println("protocol"+protocol);
    }
    //进一步解析uri
    private void parseURI(){
        /*
            uri有两种情况
            1:不含有参数的,比如:/myweb/index.html
              对于这种情况,直接将uri的值赋值给requestURI即可

            2:含有参数的,比如:/myweb/reg?username=zhangsan&password=123456&nickname=chuanqi&age=22
              2.1:先将uri按照"?"拆分为两部分.分别是"?"左边的请求部分和右边的参数部分
                  并将请求部分赋值给requestURI
                  requestURI:/myweb/reg

                  将参数部分赋值给queryString
                  queryString:username=fancq&password=123456&nickname=chuanqi&age=22

              2.2:再将参数部分继续拆分,将每组参数中的参数名作为key,参数值作为value以键值对的形式
                  存入parameters这个Map中保存
                  parameters:
                  key           value
                  username      fancq
                  password      123456
                  nickname      chuanqi
                  age           22

         */
        String[]data=uri.split("\\?");
        requestURI=data[0];
        if (data.length>1){
                queryString=data[1];
                String[]parasArr=queryString.split("\\&");
            for (String para:parasArr){
                String[]arr=para.split("=");
                parameters.put(arr[0], arr.length>1?arr[1]:null);
            }
        }

        System.out.println("requestURI:"+requestURI);
        System.out.println("queryString:"+queryString);
        System.out.println("parameters:"+parameters);
    }
    //解析消息头
    private void parseHeaders() throws IOException {
        while (true) {
           String line = readLine();
            if (line.isEmpty()){
                break;
            }
            System.out.println("消息头:" + line);
            String[]data=line.split("\\s");
            headers.put(data[0],data[1]);
        }
        System.out.println("headers:"+headers);
    }
    //解析消息正文
    private void parseContent(){}


    private String readLine() throws IOException {
        InputStream in=socket.getInputStream();
        int d;
        StringBuilder builder=new StringBuilder();
        char cur='a',pre='a';
        while ((d=in.read())!=-1){
            cur=(char)d;
            if (pre==CR&&cur==LF){
                break;
            }
            builder.append(cur);
            pre=cur;

        }
        return builder.toString().trim();
    }

    public String getMethod() {
        return method;
    }

    public String getUri() {
        return uri;
    }

    public String getProtocol(String name) {
        return headers.get(name);
    }
    public String getHeader(String name){
        return headers.get(name);
    }
    public String getRequestURI() {
        return requestURI;
    }

    public String getQueryString() {
        return queryString;
    }
    public String getParameter(String name){
        return parameters.get(name);
    }
}
